/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo.Controler;

import Model.Beans.Institution;
import Model.Servicios.InstSrv;
import Model.Servicios.UserSrv;
import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author John Mex
 */
public class AdminInstDetailsAction extends ActionSupport {
    
    private int institutionId;
    private Institution institution; 
    InstSrv intSrv = new InstSrv();
    
    public String showInstitutionDetails()
    {
        int instId = getInstitutionId();
        setInstitution(intSrv.getInstitution(instId));
        return SUCCESS;
    }
    
    public String update() {
        int instId = getInstitutionId();
        setInstitution(intSrv.getInstitution(instId));
        return "update";
    }
    
    public String complete() {
        Institution institutionUpdated = intSrv.getInstitutionById(institution.getUser().getIduser().toString());
        institutionUpdated.setName(institution.getName());
        institutionUpdated.setAddress(institution.getAddress());
        institutionUpdated.setPhone(institution.getPhone());
        institutionUpdated.getUser().setUsername(institution.getUser().getUsername());
        institutionUpdated.getUser().setPassword(institution.getUser().getPassword());
        intSrv.updateInstitution(institutionUpdated);
        new UserSrv().updateUser(institutionUpdated.getUser());
        addActionMessage("Los datos han sido actualizados!");
        return "complete";
    }

    @Override
    public void validate() {
        if (getInstitution() != null) {
            if (getInstitution().getName().equals("")) {
                addFieldError("institution.name", "Falta nombre!");
            }
            if (getInstitution().getAddress().equals("")) {
                addFieldError("institution.address", "Falta direccion!");
            }
            if (getInstitution().getPhone().equals("")) {
                addFieldError("institution.phone", "Falta telefono!");
            }
            if (getInstitution().getUser().getUsername().equals("")) {
                addFieldError("institution.user.username", "Falta usuario!");
            }
            if (getInstitution().getUser().getPassword().equals("")) {
                addFieldError("institution.user.password", "Falta password!");
            }
        }
    }
    
    public void setInstitutionId(int institutionId)
    {
        this.institutionId = institutionId;
    }
    
    public int getInstitutionId()
    {
        return institutionId;
    }
    
    public void setInstitution(Institution institution)
    {
        this.institution = institution;
    }
    
    public Institution getInstitution()
    {
        return institution;
    }
    
}
